<?php
class DataBaseConnector {

    var $createArticleTable = "CREATE TABLE IF NOT EXISTS articles (doc_id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(150), bodytext TEXT, created DATE)";
    var $createUsersTable = "CREATE TABLE IF NOT EXISTS users (name VARCHAR(50), lastname VARCHAR(50), email VARCHAR(100) PRIMARY KEY UNIQUE, password VARCHAR(50))";
    var $host = 'localhost';
    var $username = 'pw_user';
    var $password = 'pw';
    var $database = 'pw';
    var $connection;
    var $query;
    
    function connect() {
        $this->connection = mysql_connect($this->host, $this->username, $this->password) or
                die("Could not connect. " . mysql_error());
        mysql_select_db($this->database) or die("Could not select database. " . mysql_error());
    }
    
    public function selectData($table, $fields) {
        $sql = ("SELECT $fields FROM $table");       
        $this->query = mysql_query($sql) or die("Query Error");

        if (!$this->query) {
            exit("Query error: Bad Query");
        }

        if (mysql_num_rows($this->query) == 0) {
            return NULL;
        }
 
        return $this->query;
    }
    
     public function selectDataWhere($table, $fields, $conditions) {
        $sql = ("SELECT $fields FROM $table WHERE $conditions");
        $this->query = mysql_query($sql) or die("Query Error");

        if (!$this->query) {
            exit("Query error: Bad Query");
        }

        if (mysql_num_rows($this->query) == 0) {
            return NULL;
        }
        
        return $this->query;
    }
    
     public function insertData($table, $values) {
        $sql = ("INSERT INTO $table VALUES ($values)");
        $this->query = mysql_query($sql) or die("Query Error". mysql_error());
    }
    
    public function updateDataWhere($table, $fields, $conditions) {
         $sql = ("UPDATE $table SET $fields WHERE $conditions");
         $this->query = mysql_query($sql) or die("Query Error ".  mysql_error());
    }
    
    
    function freeResult(){
        mysql_free_result($this->query);
    }
}

?>